import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import ThemeContext from "./ThemeContext";
export function getColor() {
  var theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "primary";
  var _theme$colors = theme.colors,
      colors = _theme$colors === void 0 ? {} : _theme$colors;
  return colors[key] || "#4F7DE7";
} // 16进制转换成 rgba

export var hexToRgba = function hexToRgba() {
  var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var _props$theme = props.theme,
      theme = _props$theme === void 0 ? {} : _props$theme;
  if (!theme.colors || !theme.colors.primary) return "";
  var primary = theme.colors.primary;
  var rgbaColor = "rgba(".concat(parseInt("0x".concat(primary.slice(1, 3)), 16), ",").concat(parseInt("0x".concat(primary.slice(3, 5)), 16), " ,").concat(parseInt("0x".concat(primary.slice(5, 7)), 16), ",").concat(opacity, ")");
  return rgbaColor;
};
export function hexToRgbaColor() {
  var color = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "#000000";
  var opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var hex = color.slice(1);

  if (hex.length !== 3 && hex.length !== 6) {
    return "rgba(0, 0, 0, 0)";
  }

  if (hex.length === 3) {
    return "rgba(".concat(parseInt(hex.slice(0, 1), 16), ", ").concat(parseInt(hex.slice(1, 2), 16), ", ").concat(parseInt(hex.slice(2, 3), 16), ", ").concat(opacity, ")");
  }

  var r = parseInt("0x".concat(hex.slice(0, 2)), 16);
  var g = parseInt("0x".concat(hex.slice(2, 4)), 16);
  var b = parseInt("0x".concat(hex.slice(4)), 16);
  return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(opacity, ")");
} // 从currentTheme中获取某个值

export function getRequiredData() {
  var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var field = arguments.length > 1 ? arguments[1] : undefined;
  var subField = arguments.length > 2 ? arguments[2] : undefined;

  if (!props || !props.theme || !props.theme[field]) {
    return "";
  }

  if (!subField) {
    return props.theme[field] || {};
  }

  return props.theme[field][subField];
}
/**
 * 获取当前的模式
 * @param defaultValue
 * @returns {string | string}
 */

export function getSchema() {
  var defaultValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "theme1";
  var current = ThemeContext._currentValue || {};
  var _current$config = current.config,
      config = _current$config === void 0 ? {} : _current$config;
  return config.schema || defaultValue || "theme1";
}
/**
 * 解析主题数据
 * @param data 原始数据
 * @param config 配置对象
 * @returns {Object}
 */

export function resolveTheme(data, config) {
  var result = {};
  Object.keys(config).forEach(function (k) {
    if (!data[k]) {
      return;
    }

    if (typeof data[k] === "object" && data[k] !== null) {
      result[k] = data[k];
    }

    if (data[k] instanceof Array) {
      result[k] = data[k][config[k]];
    }
  });
  return result;
}
export var getChangedFields = function getChangedFields(config) {
  return Object.keys(config.current).filter(function (k) {
    return config.current[k] !== config.prev[k];
  });
}; // 解析主题数据，带diff

export function resolveThemeWithDiff() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var conf = arguments.length > 1 ? arguments[1] : undefined;
  var prevTheme = arguments.length > 2 ? arguments[2] : undefined;
  if (!Object.keys(data).length) return {};
  var current = conf.current;

  var result = _objectSpread({}, prevTheme);

  var changedField = getChangedFields(conf);
  changedField = changedField.includes("schema") ? Object.keys(conf.current) : changedField; // 如果schema发生了变化，所有样式都要重新编译

  changedField.forEach(function (field) {
    if (field === "schema") return;

    if (!data[field]) {
      return;
    }

    result = _objectSpread({}, result, _defineProperty({}, field, data[field][current[field]]));
  });
  return result;
}
/**
 * 获取插入的style标签，没有则创建
 * @param id
 * @returns {HTMLElement}
 */

export function getStyleSheetTarget(id) {
  var target = document.getElementById(id);

  if (!target) {
    target = document.createElement("style");
    target.id = id;
    document.head.appendChild(target);
  }

  return target;
}
/**
 * 对外提供的工具函数，用于获取静态资源图片url
 * @param name
 * @returns {string}
 */

export function getAsset(name) {
  // const contextValue = ThemeContext._currentValue.originTheme || {};
  var _ref = ThemeContext._currentValue || {},
      _ref$originTheme = _ref.originTheme,
      originTheme = _ref$originTheme === void 0 ? {} : _ref$originTheme,
      schema = _ref.schema;

  var _originTheme$images = originTheme.images,
      assets = _originTheme$images === void 0 ? {} : _originTheme$images; // const assets = contextValue.images || {};

  return schema === "theme2" ? undefined : assets[name];
}
export function _getAsset(name) {
  var _ref2 = ThemeContext._currentValue || {},
      _ref2$originTheme = _ref2.originTheme,
      originTheme = _ref2$originTheme === void 0 ? {} : _ref2$originTheme;

  var _originTheme$images2 = originTheme.images,
      assets = _originTheme$images2 === void 0 ? {} : _originTheme$images2;
  return assets[name];
}
export function getPrimaryColor() {
  var contextValue = ThemeContext._currentValue.originTheme || {};
  var colors = contextValue.colors || {};
  return colors.primary;
}